<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;
class ContentController extends CommonController {
    public function index(){
        $conds = [];
        $title = $_GET['title'];
        // 按标题搜索
        if($title){
            $conds['title'] = $title;
        }
        // 按照栏目搜索
        if($_GET['catid']){
            $conds['catid'] = intval($_GET['catid']);
        }
        // 分页功能
        $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
        $pageSize = 2;
        $conds['status'] = ['neq',-1];
        $news = D('News')->getNews($conds,$page,$pageSize);
        // 获取总数
        $count = D('News')->getNewsCount($conds);
        $res = new \Think\Page($count,$pageSize);
        $pageres = $res->show();
        // 推荐位操作
        $positions = D("Position")->getNormalPositions();
        // var_dump($positions);die;
        $this->assign('positions',$positions);
        $this->assign('pageres',$pageres);
        $this->assign('news',$news);
        // 获取状态为1类型为0的所有用户
        $this->assign('webSiteMenu',D("Menu")->getBarMenus());
        $this->display();
    }
    public function add(){
    	if($_POST){
    		if(!isset($_POST['title']) || !$_POST['title']){
    			return show(0,'标题不存在');
    		}
    		if(!isset($_POST['small_title']) || !$_POST['small_title']){
    			return show(0,'短标题不存在');
    		}
    		if(!isset($_POST['catid']) || !$_POST['catid']){
    			return show(0,'文章栏目不存在');
    		}
    		if(!isset($_POST['keywords']) || !$_POST['keywords']){
    			return show(0,'关键字不存在');
    		}
    		if(!isset($_POST['content']) || !$_POST['content']){
    			return show(0,'content不存在');
    		}
            // 判断是否有news_id，然后执行修改操作
            if($_POST['news_id']){
                return $this->save($_POST);
            }
            // 执行主表插入操作
    		$newsId = D('News')->insert($_POST);
            // var_dump($newsId);die;
    		if($newsId){
                // var_dump($_POST['content']);die;
    			$newsContentData['content'] = $_POST['content'];
    			$newsContentData['news_id'] = $newsId;
                // 执行副表插入操作
    			$cId = D('NewsContent')->insert($newsContentData);
    			if($cId){
    				return show(1,'新增成功');
    			}else{
    				return show(1,'主表插入成功，副表插入失败');
    			}
    		}else{
    			return show(0,'新增失败');
    		}
    	}else{
            // // 获取状态为1类型为0的所有栏目
    		$webSiteMenu = D('Menu')->getBarMenus();
            // 获取配置的标题颜色信息
	    	$titleFontColor = C("TITLE_FONT_COLOR");
            // 获取配置的来源信息
	    	$copyFrom = C("COPY_FROM");
	    	// print_r($copyFrom);die;
	    	$this->assign('webSiteMenu',$webSiteMenu);
	    	$this->assign('titleFontColor',$titleFontColor);
	    	$this->assign('copyFrom',$copyFrom);
	    	$this->display();
    	}
    }
    // 执行修改的页面显示
    public function edit($id)
    {
        if(!$id){
            $this->redirect(U('admin/content/index'));
        }
        // 获取文章的主表信息
        $news = D('News')->find($id);
        if(!$news){
            $this->redirect(U('admin/content/index'));
        }
        // 获取文章的副表信息
        $NewsContent = D('NewsContent')->find($id);
        if($NewsContent){
            $news['content'] = $NewsContent['content'];
        }
        var_dump($news);die;
        $webSiteMenu = D('Menu')->getBarMenus();
        $this->assign('webSiteMenu',$webSiteMenu);
        $this->assign('titleFontColor',C("TITLE_FONT_COLOR"));
        $this->assign('copyFrom',C("COPY_FROM"));
        $this->assign('news',$news);
        $this->display();
    }
    // 执行修改操作
    public function save($data)
    {
        $newsId = $data['news_id'];
        unset($data['news_id']);
        try{
            // 更新主表
            $id = D("News")->updateById($newsId,$data);
            $newsContentData['content'] = $data['content'];
            // 更新副表
            $condId = D("NewsContent")->updateNewsById($newsId,$newsContentData);
            if($id === false || $condId === false){
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }catch(Exception $e){
            return show(0,$e->getMessage());
        }
    }
    // 更新状态，即删除操作
    public function setStatus()
    {
        try{
            if($_POST){
                $id = $_POST['id'];
                $status = $_POST['status'];
                if(!$id){
                    return show(0,'ID不存在');
                }
                // 执行修改状态操作
                $res = D('News')->updateStatusById($id,$status);
                if($res){
                    return show(1,'操作成功');
                }else{
                    return show(0,'操作失败');
                }
            }
            return show(0,'没有提交内容');
        }catch(Exception $e){
            return show(0,$e->getMessage());
        }
        
    }
    // 执行排序操作
    public function listorder()
    {
        $listorder = $_POST['listorder'];
        // 获取当前页的前一页地址
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $errors = [];
        try{
            if($listorder){
                foreach ($listorder as $newsId => $v) {
                    // 执行排序更新操作
                    $id = D('News')->updateNewsListorderById($newsId,$v);
                    if($id === false){
                        $errors[] = $newsId;
                    }
                }
                if($errors){
                    // 将字符串连接成数组
                    return show(0,'排序失败-'.implode(',',$errors),['jump_url'=>$jumpUrl]);
                }
                return show(1,'排序成功',['jump_url'=>$jumpUrl]);
            }  
        }catch(Exception $e){
            return show(0,$e->getMessage);
        }
        return show(0,'排序数据失败',['jump_url'=>$jumpUrl]);    
    }
    // 执行推送操作
    public function push()
    {
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        // 获取所选推荐位的ID
        $positionId = intval($_POST['position_id']);
        // 获取所选内容的ID
        $newsId = $_POST['push'];
        if(!$newsId || !is_array($newsId)){
            return show(0,'请选择推荐的文章ID进行推荐');
        }
        if(!$positionId){
            return show(0,'没有选择推荐位置');
        }
        try{
            // 查询所选文本的内容
            $news = D("News")->getNewsByNewsIdIn($newsId);
            if(!$news){
                return show(0,'没有相关内容');
            }
            foreach ($news as $new) {
                $data = [
                    'position_id'=>$positionId,
                    'title'=>$new['title'],
                    'thumb'=>$new['thumb'],
                    'news_id'=>$new['news_id'],
                    'status'=>1,
                    'create_time'=>$new['create_time'],
                ];
                // var_dump($data);die;
                // 插入推荐位内容的信息
                $position = D("PositionContent")->insert($data);
                if(!$position){
                    return show(0,'插入失败');
                }
            }
        }catch(Exception $e){
            return show(0,$e->getMessage());
        }
        return show(1,'推荐成功',['jump_url'=>$jumpUrl]);
        
    }
}